package com.ffcs.cn.action.desktopcontrol.smDatasetChooser;

import com.supermap.data.Dataset;
import com.supermap.data.DatasetType;
import com.supermap.data.Datasource;
import com.supermap.desktop.controls.ui.controls.DialogResult;
import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.ui.controls.comboBox.SmComboBoxDatasource;
import com.supermap.desktop.controls.ui.controls.datasetChoose.DatasetChooseMode;
import com.supermap.desktop.controls.ui.controls.datasetChoose.SmDatasetChooser;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;
import com.supermap.desktop.core.utilties.DatasetTypeUtilities;
import com.supermap.layout.MapLayout;
import com.supermap.realspace.Scene;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class DialogSmDatasetChooser extends SmDialog {

	private JButton buttonShow;
	private JTextArea textArea;

	private JLabel labelChooseMode;
	private JList<String> listDatasetChooseMode;
	private Map<String, DatasetChooseMode> chooseModeMap;


	private JLabel labelSupportDatasetType;
	private JList<String> listSupportDatasetTypes;

	private JLabel labelSelectedMode;
	private JComboBox<String> comboBoxSelectedMode;

	private JLabel labelReadOnly;
	private JCheckBox checkBoxReadOnly;

	private JLabel labelButtonOKText;
	private JTextField textFieldSetOKText;

	private JLabel labelSelectDatasource;
	private SmComboBoxDatasource comboBoxDatasource;


	public DialogSmDatasetChooser() {
		setTitle("SmDatasetChooser 示例");
		initComponents();
		initLayout();
		initListener();
	}

	private void initListener() {
		//添加点击监听事件，显示 SmDatasetChooser
		buttonShow.addActionListener(e -> initSmDatasetChooser());

		listDatasetChooseMode.addListSelectionListener(e -> {
			List<String> list = listDatasetChooseMode.getSelectedValuesList();
			comboBoxDatasource.setEnabled(list != null && list.contains("数据集"));
		});
	}

	private void initSmDatasetChooser() {
		List<String> chooseMode = listDatasetChooseMode.getSelectedValuesList();

		DatasetChooseMode[] chooseModes = new DatasetChooseMode[chooseMode.size()];
		for (int i = 0; i < chooseModes.length; i++) {
			chooseModes[i] = chooseModeMap.get(chooseMode.get(i));
		}

		List<String> supportList = listSupportDatasetTypes.getSelectedValuesList();
		DatasetType[] datasetTypes = new DatasetType[supportList.size()];
		for (int i = 0; i < datasetTypes.length; i++) {
			datasetTypes[i] = DatasetTypeUtilities.fromString(supportList.get(i));
		}

		Datasource selectedDatasource = comboBoxDatasource.getSelectedDatasource();

		SmDatasetChooser smDatasetChooser = new SmDatasetChooser(chooseModes);
		smDatasetChooser.setSupportDatasetTypes(datasetTypes);
		smDatasetChooser.setButtonOKText(textFieldSetOKText.getText());
		smDatasetChooser.setSupportReadOnly(checkBoxReadOnly.isSelected());
		smDatasetChooser.setTableSelectionMode(comboBoxSelectedMode.getSelectedIndex());
		if (selectedDatasource != null && comboBoxDatasource.isEnabled()) {
			smDatasetChooser.setSelectedDatasource(selectedDatasource);
		}

		textArea.setText("");
		if (smDatasetChooser.showDialog() == DialogResult.OK) {
			List<Object> selectedObjects = smDatasetChooser.getSelectedObjects();
			if (selectedObjects == null || selectedObjects.size() ==0){
				textArea.append("选中对象为空");
			}else {
				for (Object selectedObject : selectedObjects) {
					if (selectedObject instanceof Dataset){
						textArea.append("数据集："+((Dataset)selectedObject).getName()+"\n");
					}else if (selectedObject instanceof MapLayout){
						textArea.append("布局："+((MapLayout)selectedObject).getName()+"\n");
					}else if (selectedObject instanceof Scene){
						textArea.append("场景："+((Scene)selectedObject).getName()+"\n");
					}else if (selectedObject instanceof com.supermap.mapping.Map){
						textArea.append("地图："+((com.supermap.mapping.Map)selectedObject).getName()+"\n");
					}
				}
			}
		}
	}

	private void initLayout() {
		this.setLayout(new GridBagLayout());
		this.add(getLeftPanel(), new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP)
				.setWeight(1, 1)
				.setFill(GridBagConstraints.BOTH));

		this.add(getRightPanel(), new GridBagConstraintsHelper(1, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1, 1)
				.setFill(GridBagConstraints.BOTH));
	}

	private void initComponents() {
		initLeftComponents();
		initRightComponents();
	}

	private void initRightComponents() {
		labelChooseMode = new JLabel("显示的节点：");
		chooseModeMap = new HashMap<>();
		chooseModeMap.put("地图", DatasetChooseMode.MAP);
		chooseModeMap.put("数据集", DatasetChooseMode.DATASET);
		chooseModeMap.put("布局", DatasetChooseMode.LAYOUT);
		chooseModeMap.put("场景", DatasetChooseMode.SCENE);
		listDatasetChooseMode = new JList<>();
		listDatasetChooseMode.setListData(new String[]{"数据集","地图", "布局", "场景"});
		listDatasetChooseMode.setSelectedIndex(0);

		labelSupportDatasetType = new JLabel("显示数据集类型：");
		listSupportDatasetTypes = new JList<>();
		DatasetType[] allDatasetType = DatasetTypeUtilities.getAllDesktopDatasetType();
		String[] supportTypes = new String[allDatasetType.length];
		for (int i = 0; i < allDatasetType.length; i++) {
			supportTypes[i] = DatasetTypeUtilities.toString(allDatasetType[i]);
		}
		listSupportDatasetTypes.setListData(supportTypes);
		listSupportDatasetTypes.setSelectedIndex(0);

		labelSelectedMode = new JLabel("选择模式：");
		comboBoxSelectedMode = new JComboBox<>();
		comboBoxSelectedMode.addItem("单选");
		comboBoxSelectedMode.addItem("连选");
		comboBoxSelectedMode.addItem("多选");
		comboBoxSelectedMode.setSelectedIndex(2);

		labelReadOnly = new JLabel("显示只读数据源：");
		checkBoxReadOnly = new JCheckBox();

		labelButtonOKText = new JLabel("确定按钮文本：");
		textFieldSetOKText = new JTextField("确定");

		labelSelectDatasource = new JLabel("选中数据源：");
		comboBoxDatasource = new SmComboBoxDatasource();
	}

	private void initLeftComponents() {
		buttonShow = new JButton("显示 SmDatasetChooser 控件");
		textArea = new JTextArea(10, 15);
		textArea.setEditable(false);
	}

	private JPanel getLeftPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		panel.setBorder(new TitledBorder("控件"));
		panel.add(buttonShow, new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST)
				.setWeight(1, 1).setFill(GridBagConstraints.HORIZONTAL));

		JScrollPane jScrollPane = new JScrollPane(textArea);
		jScrollPane.setBorder(new TitledBorder("SmDatasetChooser 选中结果"));

		JPanel leftPanel = new JPanel();
		leftPanel.setLayout(new GridBagLayout());
		leftPanel.add(panel, new GridBagConstraintsHelper(0, 0, 1, 1)
				.setWeight(1, 1).setFill(GridBagConstraints.BOTH));
		leftPanel.add(jScrollPane, new GridBagConstraintsHelper(0, 1, 1, 1)
				.setWeight(1, 1).setFill(GridBagConstraints.BOTH));

		return leftPanel;
	}

	private JPanel getRightPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		panel.add(labelButtonOKText, new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
				.setAnchor(GridBagConstraints.WEST));
		panel.add(textFieldSetOKText, new GridBagConstraintsHelper(1, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1, 1).setFill(GridBagConstraints.HORIZONTAL)
				.setAnchor(GridBagConstraints.WEST));

		panel.add(labelReadOnly, new GridBagConstraintsHelper(0, 1, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
				.setAnchor(GridBagConstraints.WEST));
		panel.add(checkBoxReadOnly, new GridBagConstraintsHelper(1, 1, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1, 1).setFill(GridBagConstraints.HORIZONTAL)
				.setAnchor(GridBagConstraints.WEST));

		panel.add(labelSelectedMode, new GridBagConstraintsHelper(0, 2, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
				.setAnchor(GridBagConstraints.WEST));
		panel.add(comboBoxSelectedMode, new GridBagConstraintsHelper(1, 2, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1, 1).setFill(GridBagConstraints.HORIZONTAL)
				.setAnchor(GridBagConstraints.WEST));

		panel.add(labelSelectDatasource, new GridBagConstraintsHelper(0, 3, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
				.setAnchor(GridBagConstraints.WEST));
		panel.add(comboBoxDatasource, new GridBagConstraintsHelper(1, 3, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1, 1).setFill(GridBagConstraints.HORIZONTAL)
				.setAnchor(GridBagConstraints.WEST));

		panel.add(labelChooseMode, new GridBagConstraintsHelper(0, 4, 2, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST));
		panel.add(listDatasetChooseMode, new GridBagConstraintsHelper(0, 5, 2, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1, 1).setFill(GridBagConstraints.HORIZONTAL)
				.setAnchor(GridBagConstraints.WEST));

		panel.add(labelSupportDatasetType, new GridBagConstraintsHelper(0, 6, 2, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST));
		JScrollPane jScrollPane = new JScrollPane(listSupportDatasetTypes);
		jScrollPane.setPreferredSize(new Dimension(100, 150));
		jScrollPane.setMinimumSize(new Dimension(100, 150));
		panel.add(jScrollPane, new GridBagConstraintsHelper(0, 7, 2, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1, 1).setFill(GridBagConstraints.BOTH)
				.setAnchor(GridBagConstraints.WEST));
		panel.setBorder(new TitledBorder("控制面板"));
		return panel;
	}

}
